package com.mlethe.android.demo.seattable.presenter;

import android.widget.Toast;

import androidx.fragment.app.FragmentActivity;

import com.mlethe.android.demo.seattable.base.BasePresenter;
import com.mlethe.android.demo.seattable.entity.CineSeat;
import com.mlethe.android.demo.seattable.entity.Obj;
import com.mlethe.android.demo.seattable.entity.Result;
import com.mlethe.android.demo.seattable.model.MovieModel;
import com.mlethe.android.demo.seattable.view.SeatTableView;
import com.mlethe.library.net.callback.Consumer;
import com.mlethe.library.widget.seattable.Seat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SeatTablePresenter extends BasePresenter<SeatTableView> {

    private MovieModel mMovieModel;
    public SeatTablePresenter(FragmentActivity activity) {
        super(activity);
        mMovieModel = new MovieModel();
    }

    public void cinemaPlaySeatStatus(String hallId, String cineUpdateTime) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("play_id", hallId);
        map.put("play_update_time", cineUpdateTime);

        mMovieModel.cinemaPlaySeatStatus(map)
                .subscribe(new Consumer<Result<Obj<List<CineSeat>>>>() {
                    @Override
                    public void onSuccess(Result<Obj<List<CineSeat>>> objResult) {
                        if (!objResult.isOk()) {
                            Toast.makeText(mFragmentActivity, objResult.getContent(), Toast.LENGTH_SHORT).show();
                        } else if (!objResult.getObject().getRes().isOk()){
                            Toast.makeText(mFragmentActivity, objResult.getObject().getRes().getErrorMessage(), Toast.LENGTH_SHORT).show();
                        } else {
                            handleSeatsData(objResult.getObject().getRes().getData());
                        }
                    }

                    @Override
                    public void onFailure(Throwable throwable) {
                        throwable.printStackTrace();
                        Toast.makeText(mFragmentActivity, "网络异常，请检查网络", Toast.LENGTH_SHORT).show();
                    }
                });
    }

    /**
     * 处理座位图数据
     * @param cineSeats
     */
    private void handleSeatsData(List<CineSeat> cineSeats) {
        List<List<Seat>> seats = new ArrayList<>();
        List<Seat> list = new ArrayList<>();
        for (int i = 0; i < cineSeats.size(); i++) {
            CineSeat cineSeat = cineSeats.get(i);
            if (seats.size() == 0) {
                seats.add(list);
            } else {
                List<Seat> rows = seats.get(seats.size() - 1);
                Seat seat = rows.get(0);
                if (seat.getX() != cineSeat.getX()) {
                    list = new ArrayList<>();
                    seats.add(list);
                }
            }
            int state = Seat.SEAT_TYPE_SOLD;
            if ("road".equals(cineSeat.getType())) {
                state = Seat.SEAT_TYPE_NOT_AVAILABLE;
            } else if ("ok".equals(cineSeat.getSeatStatus())) {
                state = Seat.SEAT_TYPE_AVAILABLE;
            }
            int type = Seat.ROAD;
            if ("danren".equals(cineSeat.getType())) {
                type = Seat.SINGLE_PERSON_SEAT;
            } else if ("shuangren".equals(cineSeat.getType())) {
                type = Seat.DOUBLE_MAN_SEAT;
                list.add(new Seat(cineSeat.getCineSeatId(), cineSeat.getX(), cineSeat.getY(),cineSeat.getRowValue(), cineSeat.getColumnValue(), type, state, cineSeat.getPairValue()));
                continue;
            } else if ("baoliu".equals(cineSeat.getType())) {
                type = Seat.RETAIN_SEAT;
            } else if ("canji".equals(cineSeat.getType())) {
                type = Seat.DEFORMITY_SEAT;
            } else if ("vip".equals(cineSeat.getType())) {
                type = Seat.VIP_SEAT;
            } else if ("zhendong".equals(cineSeat.getType())) {
                type = Seat.VIBRATE_SEAT;
            }
            list.add(new Seat(cineSeat.getCineSeatId(), cineSeat.getX(), cineSeat.getY(),cineSeat.getRowValue(), cineSeat.getColumnValue(), type, state));
        }
        getView().notifySeatTable(seats);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mMovieModel != null) {
            mMovieModel = null;
        }
    }
}
